package com.xhui.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhui.reggie.common.BaseContext;
import com.xhui.reggie.entity.AddressBook;
import com.xhui.reggie.service.AddressBookService;
import com.xhui.reggie.mapper.AddressBookMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
* @author 王辉
* @description 针对表【address_book(地址管理)】的数据库操作Service实现
* @createDate 2023-01-20 11:15:33
*/
@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook>
    implements AddressBookService{

    /**
     *
     * @param addressBook 地址簿的信息 此处传递的是 地址簿id
     */
    @Override
    @Transactional
    public void setDefault(AddressBook addressBook) {
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        wrapper.set(AddressBook::getIsDefault, 0);
        // 确保该用户的所有地址均为非默认地址 0
        // update address_book set is_default = 0 where user_id = ?
        this.update(wrapper);

        // 然后再设置某个具体的地址为默认地址 1
        // update address_book set is_default = 1 where id = ?
        addressBook.setIsDefault(1);
        this.updateById(addressBook);
    }
}




